﻿Public Class FormType
    Private Sub LoadData()
        Cmd.Parameters("@s") = "%" & Me.txtSearch.Text & "%"
        Me.DataGridView1.DataSource = Cmd.ExecuteDataTable("select * from Type where upper(TypeName) like upper(@s) and Active=1")
    End Sub
    Private Sub FormType_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.formatForm(Me)
        UI.FormatDataGrid(Me.DataGridView1)
        LoadData()
    End Sub

    Private Sub btnDelet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelet.Click
        If DataGridView1.SelectedRows.Count = 0 Then Exit Sub

        If MessageBox.Show("Are you sure want to delete this Item?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
            Exit Sub
        End If
        Dim row = Me.DataGridView1.SelectedRows(0)
        Dim id = row.Cells("TypeId").Value
        Cmd.Parameters("id") = id
        Cmd.ExecuteNonQuery("update Type set Active=0 where TypeId=@id")
        LoadData()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        FormTypeEditer.ShowDialog()
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            MsgBox("Pleas select row first")
            Exit Sub
        End If

        Dim row = Me.DataGridView1.SelectedRows(0)
        Dim frm As New FormTypeEditer()
        frm.txtId.Text = row.Cells("TypeId").Value
        frm.txtName.Text = row.Cells("TypeName").Value
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            LoadData()
        End If
    End Sub
End Class